//
//  YXMainTabBar.swift
//  YXweibo
//
//  Created by AS on 5/10/16.
//  Copyright © 2016 AS. All rights reserved.
//

import UIKit

class YXMainTabBar: UITabBar {

    
    // MARK: - 属性
    /// 1.加号按钮调用闭包
    /// 按钮点击的闭包 (() -> ())? 是一个可选
    var composeCallback:(()->())?
    
   override func layoutSubviews() {
        super.layoutSubviews()
    
    
//    view: <_UITabBarBackgroundView: 0x7fd074022ee0; frame = (0 0; 375 49); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fd071c29600>> - (null)
//    view: <UITabBarButton: 0x7fd071ebef10; frame = (2 1; 90 48); opaque = NO; layer = <CALayer: 0x7fd071e80180>>
//    view: <UITabBarButton: 0x7fd07403a550; frame = (96 1; 90 48); opaque = NO; layer = <CALayer: 0x7fd07403ab70>>
//    view: <UITabBarButton: 0x7fd07403f170; frame = (190 1; 89 48); opaque = NO; layer = <CALayer: 0x7fd07403e930>>
//    view: <UITabBarButton: 0x7fd071ec9ee0; frame = (283 1; 90 48); opaque = NO; layer = <CALayer: 0x7fd071eca580>>
//    view: <UIImageView: 0x7fd074032900; frame = (0 -0.5; 375 0.5); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fd07402f2a0>> - (null)
//    view: <_UITabBarBackgroundView: 0x7fd074022ee0; frame = (0 0; 375 49); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fd071c29600>> - (null)
//    view: <UITabBarButton: 0x7fd071ebef10; frame = (2 1; 90 48); opaque = NO; layer = <CALayer: 0x7fd071e80180>>
//    view: <UITabBarButton: 0x7fd07403a550; frame = (96 1; 90 48); opaque = NO; layer = <CALayer: 0x7fd07403ab70>>
//    view: <UITabBarButton: 0x7fd07403f170; frame = (190 1; 89 48); opaque = NO; layer = <CALayer: 0x7fd07403e930>>
//    view: <UITabBarButton: 0x7fd071ec9ee0; frame = (283 1; 90 48); opaque = NO; layer = <CALayer: 0x7fd071eca580>>
//    view: <UIImageView: 0x7fd074032900; frame = (0 -0.5; 375 0.5); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fd07402f2a0>> - (null)

     // 计算frame的宽度
    let width = self.frame.width / 5
    
    // 记录按钮的位置
    var index = 0
    for view in self.subviews
    {
       // print("view: \(view)")
        // 找到UITabBarButton,才是按钮,需要修改frame值
        // UITabBarButton是系统隐藏的类 无法调用这个类的方法 [UITabBarButton class]
        if view.isKindOfClass(NSClassFromString("UITabBarButton")!) {
            // 能到这里面来说明view 是 UITabBarButton类型
            // 重新设置frame
            view.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: self.frame.height)
            
            index++
        
            if index == 2 {
                index++
            }
        
        }
    }
    
    // 设置加好按钮的位置
    composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: self.frame.height)
    }
    
     // MARK - 按钮点击事件
    func composeButtonClick(button:UIButton)
    {
        // 最终需要去Modal出控制器,View不能Modal出控制器,只有控制器才能Modal出控制器,当按钮点击的时候需要让控制器去做事情
        // 3.调用闭包
        // 调用闭包, ? 如果闭包没有值,就不执行
        composeCallback?()
        
    }
    
    // MARK: - 懒加载
    
    lazy var composeButton:UIButton = {
        
         // 创建按钮
        let button = UIButton(type:UIButtonType.Custom)
        
        // 设置按钮
        // 背景图片
     button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
     button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 按钮的图片 tabbar_compose_icon_add_highlighted
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
    button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        self.addSubview(button)
        //添加按钮点击事件
        button.addTarget(self, action: "composeButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
    }()
}
